<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>小程序直播总结 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="小程序,直播,">
  

  <meta name="description" content="一、小程序直播功能目前有哪些实现方案 内嵌腾讯直播H5：腾讯直播已改名NOW直播，小程序接入工具文档无法找到 小程序原生实现：小程序对直播和websocket都进行了比较好的封装，通过live-pusher live-player组件和websocket API即可实现直播互动功能 接入小程序直播插件实现：小程序直播，是微信提供给小程序开发者的直播组件。  二、小程序原生实现直播功能流程 微信小程">
<meta name="keywords" content="小程序,直播">
<meta property="og:type" content="article">
<meta property="og:title" content="小程序直播总结">
<meta property="og:url" content="http://blog.poetries.top/2020/06/14/weapp-live-summary/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="一、小程序直播功能目前有哪些实现方案 内嵌腾讯直播H5：腾讯直播已改名NOW直播，小程序接入工具文档无法找到 小程序原生实现：小程序对直播和websocket都进行了比较好的封装，通过live-pusher live-player组件和websocket API即可实现直播互动功能 接入小程序直播插件实现：小程序直播，是微信提供给小程序开发者的直播组件。  二、小程序原生实现直播功能流程 微信小程">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2020/06/8.webp">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2020/06/9.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2020/06/7.webp">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2020/06/1.jpg">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2020/06/2.jpg">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2020/06/10.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2020/06/11.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2020/06/12.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2020/06/13.png">
<meta property="og:updated_time" content="2020-08-15T04:25:31.942Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="小程序直播总结">
<meta name="twitter:description" content="一、小程序直播功能目前有哪些实现方案 内嵌腾讯直播H5：腾讯直播已改名NOW直播，小程序接入工具文档无法找到 小程序原生实现：小程序对直播和websocket都进行了比较好的封装，通过live-pusher live-player组件和websocket API即可实现直播互动功能 接入小程序直播插件实现：小程序直播，是微信提供给小程序开发者的直播组件。  二、小程序原生实现直播功能流程 微信小程">
<meta name="twitter:image" content="https://poetries1.gitee.io/img-repo/2020/06/8.webp">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、小程序直播功能目前有哪些实现方案"><span class="toc-text">一、小程序直播功能目前有哪些实现方案</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、小程序原生实现直播功能流程"><span class="toc-text">二、小程序原生实现直播功能流程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三、小程序直播实现过程"><span class="toc-text">三、小程序直播实现过程</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-live-player"><span class="toc-text">3.1 live-player</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-live-pusher"><span class="toc-text">3.2 live-pusher</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-服务端的选择"><span class="toc-text">3.3 服务端的选择</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-直播回放功能"><span class="toc-text">3.4 直播回放功能</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四、即时通信-IM"><span class="toc-text">四、即时通信 IM</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#五、完整示例"><span class="toc-text">五、完整示例</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-weapp-live-summary" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">小程序直播总结</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2020.06.14</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 1.9k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 7分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h2 id="一、小程序直播功能目前有哪些实现方案"><a href="#一、小程序直播功能目前有哪些实现方案" class="headerlink" title="一、小程序直播功能目前有哪些实现方案"></a>一、小程序直播功能目前有哪些实现方案</h2><ul>
<li>内嵌腾讯直播H5：腾讯直播已改名NOW直播，小程序接入工具文档无法找到</li>
<li>小程序原生实现：小程序对直播和<code>websocket</code>都进行了比较好的封装，通过<code>live-pusher</code> <code>live-player</code>组件和<code>websocket API</code>即可实现直播互动功能</li>
<li>接入小程序直播插件实现：小程序直播，是微信提供给小程序开发者的直播组件。</li>
</ul>
<h2 id="二、小程序原生实现直播功能流程"><a href="#二、小程序原生实现直播功能流程" class="headerlink" title="二、小程序原生实现直播功能流程"></a>二、小程序原生实现直播功能流程</h2><blockquote>
<p>微信小程序从<code>1.7</code>开始，为开发者提供了两个新接口，<code>&lt;live-pusher&gt;</code>和<code>&lt;live-player&gt;</code>，可以在小程序上实现单向的直播功能。通过与技术的结合，比如<code>WebRTC</code>，开发者们还可以进一步在小程序直播的基础上实现连麦功能</p>
</blockquote>
<blockquote>
<p>首先由于微信对小程序直播功能类目有限制，限定了特定类目的小程序使用</p>
</blockquote>
<p><img src="https://poetries1.gitee.io/img-repo/2020/06/8.webp" alt></p>
<p><strong>另外需要注意的两点：</strong></p>
<ul>
<li>个人号无法申请使用直播功能；</li>
<li>社交类目开通直播功能需要相关视频许可和文网文资质许可；</li>
<li>所以小程序开通直播的业务，要根据产品的目的和场景去申请对应的类目。</li>
</ul>
<blockquote>
<p>在小程序管理后台，「开发」-「接口设置」中自助开通对应的权限，如下图所示：</p>
</blockquote>
<p><img src="https://poetries1.gitee.io/img-repo/2020/06/9.png" alt></p>
<p><img src="https://poetries1.gitee.io/img-repo/2020/06/7.webp" alt></p>
<blockquote>
<p>微信小程序原生实现直播功能流程如上图所示，录制端小程序通过<code>[live-pusher]</code>组件对手机和麦克风的数据进行采集和编码推流到服务器，服务器端对数据进行加工处理并分发给多个客户端，播放端小程序通过<code>[live-player]</code>组件从云端拉流并进行实时无差异的解码和渲染，从而实现直播小程序完整互动功能</p>
</blockquote>
<p><strong>1. 推流 API 调用流程图</strong></p>
<p><img src="https://poetries1.gitee.io/img-repo/2020/06/1.jpg" alt></p>
<p><strong>2. 拉流 API 调用流程图</strong></p>
<p><img src="https://poetries1.gitee.io/img-repo/2020/06/2.jpg" alt></p>
<h2 id="三、小程序直播实现过程"><a href="#三、小程序直播实现过程" class="headerlink" title="三、小程序直播实现过程"></a>三、小程序直播实现过程</h2><blockquote>
<p>微信小程序中的推拉流功能，需要用到微信提供的 <code>live-player</code> <code>live-pusher</code> 标签</p>
</blockquote>
<h3 id="3-1-live-player"><a href="#3-1-live-player" class="headerlink" title="3.1 live-player"></a>3.1 live-player</h3><blockquote>
<p><code>live-player</code> 是微信提供的支持实时音视频播放的组件，<a href="https://developers.weixin.qq.com/miniprogram/dev/component/live-player.html" target="_blank" rel="noopener">官方介绍详见组件介绍</a>。</p>
</blockquote>
<p>创建 live-player 的演示源码如下</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;live-player</span><br><span class="line">    autoplay</span><br><span class="line">    wx:<span class="keyword">if</span>=<span class="string">"&#123;&#123;item.playUrl&#125;&#125;"</span></span><br><span class="line">    id=<span class="string">"&#123;&#123;item.streamID&#125;&#125;"</span></span><br><span class="line">    mode=<span class="string">"RTC"</span></span><br><span class="line">    object-fit=<span class="string">"fillCrop"</span></span><br><span class="line">    min-cache=<span class="string">"0.1"</span></span><br><span class="line">    max-cache=<span class="string">"0.3"</span></span><br><span class="line">    src=<span class="string">"&#123;&#123;item.playUrl&#125;&#125;"</span></span><br><span class="line">    debug=<span class="string">"&#123;&#123;pushConfig.showLog&#125;&#125;"</span></span><br><span class="line">    bindstatechange=<span class="string">"onPlayStateChange"</span></span><br><span class="line">    bindnetstatus=<span class="string">"onPlayNetStateChange"</span></span><br><span class="line">    binderror=<span class="string">"error"</span>&gt;</span><br><span class="line">    &lt;cover-view <span class="class"><span class="keyword">class</span></span>=<span class="string">'character'</span> style=<span class="string">'padding: 0 5px;'</span>&gt;&#123;&#123;item.streamID&#125;&#125;&lt;<span class="regexp">/cover-view&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>live-player&gt;</span><br></pre></td></tr></table></figure>
<p><strong>请注意</strong></p>
<ul>
<li><code>live</code> 模式主要用于直播类场景，比如赛事直播、在线教育、远程培训等等。该模式下，小程序内部的模块会优先保证观看体验的流畅，通过调整 <code>min-cache</code> 和 <code>max-cache</code> 属性，您可以调节观众(播放)端所感受到的时间延迟的大小，文档下面会详细介绍这两个参数</li>
<li><code>RTC</code> 则主要用于双向视频通话或多人视频通话场景，比如金融开会、在线客服、车险定损、培训会议 等等。在此模式下，对 <code>min-cache</code> 和 <code>max-cache</code> 的设置不会起作用，因为小程序内部会自动将延迟控制在一个很低的水平(<code>500ms</code>左右)。</li>
</ul>
<h3 id="3-2-live-pusher"><a href="#3-2-live-pusher" class="headerlink" title="3.2 live-pusher"></a>3.2 live-pusher</h3><blockquote>
<p>live-pusher 是微信提供的支持实时音视频录制的组件，<a href="https://developers.weixin.qq.com/miniprogram/dev/component/live-pusher.html" target="_blank" rel="noopener">官方介绍详见：组件介绍</a></p>
</blockquote>
<blockquote>
<p>创建 <code>live-pusher</code> 的演示源码如下</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;live-pusher</span><br><span class="line">    wx:<span class="keyword">if</span>=<span class="string">"&#123;&#123;pushUrl&#125;&#125;"</span></span><br><span class="line">    id=<span class="string">"video-livePusher"</span></span><br><span class="line">    mode=<span class="string">"RTC"</span></span><br><span class="line">    url=<span class="string">"&#123;&#123;pushUrl&#125;&#125;"</span></span><br><span class="line">    min-bitrate=<span class="string">"&#123;&#123;pushConfig.minBitrate&#125;&#125;"</span></span><br><span class="line">    max-bitrate=<span class="string">"&#123;&#123;pushConfig.maxBitrate&#125;&#125;"</span></span><br><span class="line">    aspect=<span class="string">"&#123;&#123;pushConfig.aspect&#125;&#125;"</span></span><br><span class="line">    beauty=<span class="string">"&#123;&#123;pushConfig.isBeauty&#125;&#125;"</span></span><br><span class="line">    muted=<span class="string">"&#123;&#123;pushConfig.isMute&#125;&#125;"</span></span><br><span class="line">    background-mute=<span class="string">"true"</span></span><br><span class="line">    debug=<span class="string">"&#123;&#123;pushConfig.showLog&#125;&#125;"</span></span><br><span class="line">    bindstatechange=<span class="string">"onPushStateChange"</span></span><br><span class="line">    bindnetstatus=<span class="string">"onPushNetStateChange"</span>&gt;</span><br><span class="line">    &lt;cover-view <span class="class"><span class="keyword">class</span></span>=<span class="string">'character'</span> style=<span class="string">'padding: 0 5px;'</span>&gt;&#123;&#123;isPublishing ? <span class="string">"我("</span> + publishStreamID + <span class="string">")"</span>: <span class="string">""</span>&#125;&#125;&lt;<span class="regexp">/cover-view&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>live-pusher&gt;</span><br></pre></td></tr></table></figure>
<p><strong>请注意：</strong></p>
<ul>
<li>SD、HD 和 FHD 主要用于直播类场景，比如赛事直播、在线教育、远程培训等等。SD、HD 和 FHD 分别对应三种默认的清晰度。该模式下，小程序会更加注重清晰度和观看的流畅性，不会过分强调低延迟，也不会为了延迟牺牲画质和流畅性</li>
<li>RTC 则主要用于双向视频通话或多人视频通话场景，比如金融开会、在线客服、车险定损、培训会议 等等。该模式下，小程序会更加注重降低点到点的时延，也会优先保证声音的质量，在必要的时候会对画面清晰度和画面的流畅性进行一定的缩水</li>
</ul>
<h3 id="3-3-服务端的选择"><a href="#3-3-服务端的选择" class="headerlink" title="3.3 服务端的选择"></a>3.3 服务端的选择</h3><blockquote>
<p>由于自己搭<code>rtmp</code>（例如<code>Nginx rtmp</code>）成本较高，技术实现难度大，考虑云服务商提供的视频直播服务产品，生成推流地址和播放地址，目前市面上主流的云直播产品有：腾讯云、阿里云、七牛云等。</p>
</blockquote>
<p><img src="https://poetries1.gitee.io/img-repo/2020/06/10.png" alt></p>
<blockquote>
<p>各平台均提供内容接入与分发和分布式实时视频处理技术，每个平台提供的功能大同小异但各有千秋，平均费用大概 20-30元/100G，100G流量可以满足100人同时在线直播4小时</p>
</blockquote>
<p><strong>接下来选择腾讯云直播进行接入体验</strong></p>
<ol>
<li>申请腾讯云账号，开通云直播权限，它会赠送20gb流量给你 超出需要自己花钱。开通流程请参考下面的文档：<a href="https://cloud.tencent.com/document/product/454/12517" target="_blank" rel="noopener">https://cloud.tencent.com/document/product/454/12517</a></li>
<li>域名管理，在这里面会看到两个域名 一个是推流域名 一个是播放域名，域名可以用自己的 （建议配置自己的域名，2019/2/26上线查看时发现赠送的播放域名已失效）。具体看下面的文档 <a href="https://cloud.tencent.com/document/product/267/20381" target="_blank" rel="noopener">https://cloud.tencent.com/document/product/267/20381</a></li>
</ol>
<p><img src="https://poetries1.gitee.io/img-repo/2020/06/11.png" alt></p>
<blockquote>
<p>由于腾讯云不再赠送播放域名，所以需要租用或者使用自己的域名生成播放地址，自己的播放域名不能直接访问，需要完成CNAME配置</p>
</blockquote>
<p><img src="https://poetries1.gitee.io/img-repo/2020/06/12.png" alt></p>
<ol start="3">
<li>最后，小程序直播组件API接入</li>
</ol>
<blockquote>
<p>第一步：<code>&lt;live-pusher&gt;</code>推流（数据包实时上传）示例代码：</p>
</blockquote>
<p>使用<code>&lt;live-pusher&gt;</code>发布流，这里使用的参数 <code>min-bitrate=&quot;200&quot;</code> 最小码率 <code>max-bitrate=&quot;400&quot;</code> 最大码率 <code>mode=&quot;RTC&quot;</code> RTC模式，加入房间之后我们需要调用 <code>publish</code>返回一个 <code>rtmp</code> 推流地址</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;live-pusher</span><br><span class="line">  autopush</span><br><span class="line">  min-bitrate=<span class="string">"200"</span></span><br><span class="line">  max-bitrate=<span class="string">"400"</span></span><br><span class="line">  mode=<span class="string">"RTC"</span></span><br><span class="line">  url=<span class="string">"&#123;&#123;publishPath&#125;&#125;"</span>&gt;</span><br><span class="line">&lt;<span class="regexp">/live-pusher&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>先使用 <code>wx.createLivePusherContext</code> 创建 <code>LivePusherContext</code>，再使用 setData 设置好 publishPath 之后发布</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// index.js</span></span><br><span class="line"></span><br><span class="line">Page(&#123;</span><br><span class="line">  data: &#123;</span><br><span class="line">    publishPath: <span class="literal">undefined</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">  publish() &#123;</span><br><span class="line">  <span class="comment">// joinRoom 之后调用</span></span><br><span class="line">  <span class="comment">// 创建 LivePusherContext</span></span><br><span class="line">  <span class="keyword">const</span> pushContext = wx.createLivePusherContext()</span><br><span class="line">  <span class="keyword">const</span> path = session.publish()</span><br><span class="line">  <span class="keyword">this</span>.setData(</span><br><span class="line">    &#123; <span class="attr">publishPath</span>: path &#125;,</span><br><span class="line">    () =&gt; &#123;</span><br><span class="line">      pushContext.start(&#123;</span><br><span class="line">          success: <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">'推流成功'</span>)</span><br><span class="line">          &#125;,</span><br><span class="line">          fail: <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">'推流开始失败'</span>)</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>第二步：<code>&lt;live-player&gt;</code>播放（数据包实时下载）示例代码：</p>
</blockquote>
<p>使用<code>&lt;live-player&gt;</code>订阅流，加入房间之后我们可以调用 <code>subscribe</code> 返回一个 <code>rtmp</code> 拉流地址。 下面我们使用了 <code>wx:for</code> 遍历 <code>data.subscribeList</code> 渲染一个订阅的列表</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;live-player</span><br><span class="line">  autoplay</span><br><span class="line">  wx:key=<span class="string">"&#123;&#123;item.key&#125;&#125;"</span></span><br><span class="line">  wx:<span class="keyword">for</span>=<span class="string">"&#123;&#123;subscribeList&#125;&#125;"</span></span><br><span class="line">  min-cache=<span class="string">"0.2"</span></span><br><span class="line">  max-cache=<span class="string">"0.8"</span></span><br><span class="line">  src=<span class="string">"&#123;&#123;item.url&#125;&#125;"</span></span><br><span class="line">  mode=<span class="string">"RTC"</span>&gt;</span><br><span class="line">&lt;<span class="regexp">/live-player&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="3-4-直播回放功能"><a href="#3-4-直播回放功能" class="headerlink" title="3.4 直播回放功能"></a>3.4 直播回放功能</h3><blockquote>
<p>参考 <a href="https://cloud.tencent.com/document/product/454/8681#1.-.E7.9B.B4.E6.92.AD.E5.BD.95.E5.88.B6.E7.9A.84.E5.8E.9F.E7.90.86.E6.98.AF.E4.BB.80.E4.B9.88.EF.BC.9F" target="_blank" rel="noopener">腾讯云</a>接入实现，一般是后台来实现</p>
</blockquote>
<h2 id="四、即时通信-IM"><a href="#四、即时通信-IM" class="headerlink" title="四、即时通信 IM"></a>四、即时通信 IM</h2><blockquote>
<p>在直播中加入Im功能，参考<a href="https://cloud.tencent.com/document/product/269" target="_blank" rel="noopener">腾讯云IM</a>接入</p>
</blockquote>
<ul>
<li><a href="https://github.com/tencentyun/TIMSDK/tree/master/WXMini" target="_blank" rel="noopener">https://github.com/tencentyun/TIMSDK/tree/master/WXMini</a></li>
<li><a href="https://cloud.tencent.com/document/product/269/37448" target="_blank" rel="noopener">https://cloud.tencent.com/document/product/269/37448</a></li>
<li><a href="https://imsdk-1252463788.file.myqcloud.com/IM_DOC/Web/SDK.html#createTextMessage" target="_blank" rel="noopener">IM sdk文档</a></li>
</ul>
<h2 id="五、完整示例"><a href="#五、完整示例" class="headerlink" title="五、完整示例"></a>五、完整示例</h2><blockquote>
<p>实现效果</p>
</blockquote>
<p><img src="https://poetries1.gitee.io/img-repo/2020/06/13.png" alt></p>
<blockquote>
<p>部分代码参考 <a href="https://github.com/poetries/weapp-live" target="_blank" rel="noopener">https://github.com/poetries/weapp-live</a></p>
</blockquote>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2020/05/24/wx-share/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2020/07/24/weapp-h5-jump/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
